<!DOCTYPE html>
<html>
<head>
    <title>企业实名综合申请</title>
    <link rel="stylesheet" type="text/css" href="style.css">
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/js-base64@3.6.0/base64.min.js"></script>
    <meta charset="utf-8">
    
</head>
<body>
    <div class="container">
        <div class="form-container">
            <h2><a href="./personBasic.html" >个人实名综合申请</a> 企业实名综合申请 </h2>
            
            <label for="request-url">请求地址：</label>
            <select id="environment">
                <option selected>天威云预演环境</option>
                <option>天威云生产环境</option>
            </select>
            <input type="text" id="request-url" disabled="disabled" value="https://demo-open.itruscloud.com/apigate/authapi/enterprise/authIden">
            <pre></pre>


            <label for="request-url">选择产品：</label>
            <select id="product">
                <option value="idb0003" selected>企业工商信息详情版（idb0003）</option>
                <option value="idb0007">企业登记信息详情版（idb0007）</option>

                <option value="idb0006">企业工商及法人姓名信息详情版（idb0006）</option>

                <option value="idb0011">企业工商以及法人信息详情版（idb0011）</option>
                <option value="idb0015">企业登记以及法人信息详情版（idb0015）</option>

                <option value="idb0019">企业工商以及代理人信息详情版（idb0019）</option>
                <option value="idb0023">企业登记以及代理人信息详情版（idb0023）</option>

                <option value="idb0027">企业工商、法人以及代理人信息详情版（idb0027）</option>
                <option value="idb0031">企业登记、法人以及代理人信息详情版（idb0031）</option>

            </select>
            <pre></pre>

            
            <table id="data-container" style="width: 70%;">
                <label for="request-url">业务数据：</label><pre></pre>
                <div id="basic"> 
                    <tr>
                        <td style="width: 20%;">APP_ID：</td>
                        <td>
                        <input type="text" id="app-id" value="">
                        </td>
                    </tr>
                    <tr>
                        <td>SECRET_KEY：</td>
                        <td>
                            <input type="text" id="secret-key" value="">
                        </td>
                    </tr>
                </div>  

                <tr>
                    <td style="width: 15%;">企业名称：</td>
                    <td>
                    <input type="text" id="company-name" name="name" value="">
                    </td>
                </tr>
                <tr>
                    <td>企业类型：</td>
                    <td>    
                        <select id="company-type" name="type" style="width: 50%;">
                            <option value="ET_PE" selected>企业（ET_PE）</option>
                            <option value="ET_SE">个体工商户（ET_SE）</option>
                            <option value="ET_OU">政府机构/事业单位（ET_OU）</option>
                        </select>
                    </td>
                </tr>

                

                <tr>
                    <td>统一社会信用代码或营业执照注册号：</td>
                    <td>
                        <input type="text" id="company-idcode" name="idCode" value="">
                    </td>
                </tr>

                <tr> 
                    <td>组织机构代码：</td>
                    <td>
                        <input type="text" id="company-orgcode" name="orgCode" value="">
                    </td>
                </tr>

                <tr id="legal-name"> 
                    <td>法定代表人姓名：</td>
                    <td>
                        <input type="text" id="company-legal-name" name="legalName" value="">
                    </td>
                </tr>
                <tr id="legal-id"> 
                    <td>法定代表人身份证号:</td>
                    <td>
                        <input type="text" id="company-legal-id" name="legalId" value="">
                    </td>
                </tr>

                <tr id="agent-name"> 
                    <td>代理人姓名：</td>
                    <td>
                        <input type="text" id="company-agent-name" name="agentName" value="">
                    </td>
                </tr>
                <tr id="agent-id"> 
                    <td>代理人身份证号：</td>
                    <td>
                        <input type="text" id="company-agent-id" name="agentId" value="">
                    </td>
                </tr>
                
                
            </table>
            
            <input for="request-data" id="create-request-data" type="submit" value="生成请求数据">
            <pre></pre>
            <pre></pre>
            请求体:<textarea id="request-data" rows="2" cols="40" disabled="disabled"> </textarea>
            签名值:<span style="color: gray;"> 使用 secretKey + serviceCode 作为秘钥，对 JSON 请求体进行 HMAC-SHA1 哈希计算</span><textarea id="request-signature" rows="1" cols="40" disabled="disabled"> </textarea>
            <pre></pre>
           
            请求 curl 命令：<textarea id="request-curl" rows="8" cols="40" disabled="disabled"> </textarea>
            
             
        </div>
    </div>

    <script>
        $(function(){
            // 设置请求地址
            $('#environment').on('change', (e) => {
                let select = $('#environment option:selected').text();
                $("#request-url").val("https://demo-open.itruscloud.com/apigate/authapi/person/authIden");
                if(select == "天威云生产环境"){
                    $("#request-url").val("https://open.itruscloud.com/apigate/authapi/person/authIden");
                } 
            });

            $("#legal-name").hide();
            $("#legal-id").hide();
            
            $("#agent-name").hide();
            $("#agent-id").hide();

            $('#product').on('change', (e) => {
                let select = $('#product option:selected').val();

                $("#legal-name").hide();
                $("#legal-id").hide();
                
                $("#agent-name").hide();
                $("#agent-id").hide();

                if(select == "idb0006"){
                    $("#legal-name").show();
                } else if(select == "idb0011" || select == "idb0015"){
                    $("#legal-name").show();
                    $("#legal-id").show();
                } else if(select == "idb0019" || select == "idb0023"){
                    $("#agent-name").show();
                    $("#agent-id").show();
                    
                } else if(select == "idb0027" || select == "idb0031"){
                    $("#legal-name").show();
                    $("#legal-id").show();

                    $("#agent-name").show();
                    $("#agent-id").show();
                }
            });



            $('#create-request-data').click(function(){
                let url = $('#request-url').val();

                let appId = $('#app-id').val();
                let serviceCode = $('#product option:selected').val();
                let requestJson = {
                    "appId": appId,
                    "serviceCode": serviceCode
                };

                let inputs = $('#data-container').find("input")
                    .filter(':visible')
                    .filter(function() {return this.id != 'app-id';})
                    .filter(function() {return this.id != 'secret-key';});


                inputs.each(function(j){
                    if($(this).attr("type") == 'text'){
                        // 添加 input 数据到请求 json 中
                        requestJson[$(this).attr('name')] = $(this).val();
                    }
                });

                requestJson[$('#company-type').attr('name')] = $('#company-type option:selected').val();
                $('#request-data').val(JSON.stringify(requestJson));

                let secretKey = $('#secret-key').val();

                $.ajax({
                    url: "http://101.40.86.116:31915/public/sign",
                    type: 'post',
                    data: "key=" + secretKey + serviceCode + "&data=" + JSON.stringify(requestJson),
                    success: function(res){
                        let signature = res.data;
                        $('#request-signature').val(signature);

                        let curl = "curl --location '" + url + "' \n" +
                            "--header 'Authorization: COMMON " + signature + "'\n" +
                            "--header 'Content-Type: application/json' \n" +
                            "--header 'appId: " + appId + "'\n" +
                            "--header 'serviceCode: " + serviceCode + "'\n" +
                            "--data '" + JSON.stringify(requestJson) + "'";

                        $('#request-curl').val(curl);
                    },
                    error: function(res){
                        console.log('请求失败');
                        $("#request-signature").html(JSON.stringify(res));
                    }
                });

                
                
            });
        });

        function hasKey(json, key) {
            return key in json || json.hasOwnProperty(key) || typeof(json[key]) !== "undefined";
        }

        
    </script>
</body>
</html>